package com.echo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.echo.util.DisplayUtil;

public class CircleBar extends View {

	private RectF mColorWheelRectangle = new RectF();
	private Paint mDefaultWheelPaint;
	private Paint mColorWheelPaint;
	private Paint textPaint;
	private Paint textPaint1;
	private float mColorWheelRadius;
	private float circleStrokeWidth;
	private float pressExtraStrokeWidth;
	private int mText;
	private int mCount;
	private float mSpeed;
	private float mProgressAni;
	private float mProgress;
	private int mTextSize;
	private int mTextSize2;
	private int mTextSize1;
	private int mDistance;
	private int mDistance1;
	BarAnimation anim;
	private int mType;
	private int max = 10000;

	public CircleBar(Context context) {
		super(context);
		init();
	}

	public CircleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public CircleBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {

		circleStrokeWidth = DisplayUtil.dip2px(getContext(), 20);
		mTextSize = DisplayUtil.sp2px(getContext(), 80);
		mTextSize2 = DisplayUtil.sp2px(getContext(), 50);
		mTextSize1 = DisplayUtil.sp2px(getContext(), 25);
		mDistance = DisplayUtil.dip2px(getContext(), 60);
		mDistance1 = DisplayUtil.dip2px(getContext(), 20);


		mColorWheelPaint = new Paint();
		mColorWheelPaint.setAntiAlias(true);
		mColorWheelPaint.setColor(0xFF29a6f6);
		mColorWheelPaint.setStyle(Paint.Style.STROKE);
		mColorWheelPaint.setStrokeCap(Paint.Cap.ROUND);
		mColorWheelPaint.setStrokeWidth(circleStrokeWidth);


		mDefaultWheelPaint = new Paint();
		mDefaultWheelPaint.setAntiAlias(true);
		mDefaultWheelPaint.setColor(0xFFeeefef);
		mDefaultWheelPaint.setStyle(Paint.Style.STROKE);
		mDefaultWheelPaint.setStrokeWidth(circleStrokeWidth);


		textPaint = new Paint(Paint.LINEAR_TEXT_FLAG);
		textPaint.setAntiAlias(true);
		textPaint.setColor(Color.parseColor("#6DCAEC"));
		textPaint.setStyle(Style.FILL_AND_STROKE);
		textPaint.setTextAlign(Align.LEFT);
		textPaint.setTextSize(mTextSize);


		textPaint1 = new Paint(Paint.LINEAR_TEXT_FLAG);
		textPaint1.setAntiAlias(true);
		textPaint1.setColor(Color.parseColor("#a1a3a6"));
		textPaint1.setStyle(Style.FILL_AND_STROKE);
		textPaint1.setTextAlign(Align.LEFT);
		textPaint1.setTextSize(mTextSize1);


		mText = 0;

		mProgress = 0;


		anim = new BarAnimation();
		anim.setDuration(1000);

	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		int halfHeight = getHeight() / 2;
		int halfWidth = getWidth() / 2;
		int radius = halfHeight < halfWidth ? halfHeight : halfWidth;

		canvas.drawCircle(halfWidth, halfHeight, radius - mDistance1,
				mDefaultWheelPaint);


		mColorWheelRectangle.top = halfHeight - radius + mDistance1;
		mColorWheelRectangle.bottom = halfHeight + radius - mDistance1;
		mColorWheelRectangle.left = halfWidth - radius + mDistance1;
		mColorWheelRectangle.right = halfWidth + radius - mDistance1;

		canvas.drawArc(mColorWheelRectangle, -90, mProgressAni, false,
				mColorWheelPaint);
		Rect bounds = new Rect();
		String middleText = "";
		String upText = "";
		String downText = "";
		String bottomText="";

		if (this.mType == 1) {
			upText = "step";
			downText = "speed";
			middleText = String.valueOf(mCount);
			bottomText=String.valueOf(mSpeed);
		} else if (this.mType == 2) {
			upText = "heat";
			downText = "speed";
			middleText = String.valueOf(mCount);
		}
		textPaint.getTextBounds(middleText, 0, middleText.length(), bounds);

		canvas.drawText(middleText, (mColorWheelRectangle.centerX())
						- (textPaint.measureText(middleText) / 2),
				mColorWheelRectangle.centerY() + bounds.height() / 2, textPaint);
		textPaint1.getTextBounds(upText, 0, upText.length(), bounds);
		canvas.drawText(
				upText,
				(mColorWheelRectangle.centerX())
						- (textPaint1.measureText(upText) / 2),
				mColorWheelRectangle.centerY() + bounds.height() / 2
						- mDistance, textPaint1);
		textPaint1.getTextBounds(downText, 0, downText.length(), bounds);
		canvas.drawText(downText, (mColorWheelRectangle.centerX())
						- (textPaint1.measureText(downText) / 2),
				mColorWheelRectangle.centerY() + bounds.height() / 2
						+ mDistance, textPaint1);
		textPaint1.getTextBounds(bottomText,0,bottomText.length(),bounds);
		canvas.drawText(bottomText, (mColorWheelRectangle.centerX())
						- (textPaint1.measureText(bottomText) / 2),
				mColorWheelRectangle.centerY() + bounds.height()*2
						+ mDistance, textPaint1);
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int height = getDefaultSize(getSuggestedMinimumHeight(),
				heightMeasureSpec);
		int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
		int min = Math.min(width, height);
		setMeasuredDimension(min, min);
		mColorWheelRadius = min - circleStrokeWidth - pressExtraStrokeWidth;

		mColorWheelRectangle.set(circleStrokeWidth + pressExtraStrokeWidth,
				circleStrokeWidth + pressExtraStrokeWidth, mColorWheelRadius,
				mColorWheelRadius);
	}

	public void startCustomAnimation() {
		this.startAnimation(anim);
	}

	public void setMax(int max) {
		this.max = max;
	}

	public void setProgress(int progress, int mType,float speed) {

		if (this.mType != mType) {
			this.startCustomAnimation();
			mProgress = (Float.valueOf(progress) / Float.valueOf(max)) * 360;
			mText = progress;
			this.mSpeed=speed;
			this.mType = mType;
		} else {
			this.mCount = progress;
			this.mProgressAni = (Float.valueOf(progress) / Float.valueOf(max)) * 360;
		}

		this.postInvalidate();
	}

	public void setBottom(float speed){
		this.mSpeed=speed;
		this.postInvalidate();
	}
	public class BarAnimation extends Animation {

		@Override
		protected void applyTransformation(float interpolatedTime,
										   Transformation t) {
			super.applyTransformation(interpolatedTime, t);
			if (interpolatedTime < 1.0f) {
				mProgressAni = interpolatedTime * mProgress;
				mCount = (int) (interpolatedTime * mText);
			} else {
				mProgressAni = mProgress;
				mCount = mText;
			}
			postInvalidate();

		}
	}

}
